package com.example.demo.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.widget.Toast;

import com.example.demo.R;
import com.example.demo.data.model.CurrentUser;
import com.example.demo.data.model.Users;
import com.example.demo.databinding.ActivityLoginBinding;


public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        bindLogin();
        bindRegister();
    }



    private void bindRegister() {
        binding.loginRegister.setOnClickListener(v -> {
            startActivity(new Intent(this, RegisterActivity.class));
        });
    }

    private void bindLogin() {
        binding.loginBtn.setOnClickListener(v -> {
            String name = binding.username.getText().toString();
            String pwd = binding.password.getText().toString();
            String realPwd = Users.users.get(name);
            if (realPwd != null && realPwd.equals(pwd)) {
                Toast.makeText(getApplicationContext(), R.string.login_success, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, MainActivity.class));
                CurrentUser.curUser = name;
            } else {
                Toast.makeText(getApplicationContext(), R.string.login_failed, Toast.LENGTH_SHORT).show();
            }

        });
    }
}